<template>
  <div class="nav-menu">
    <div class="logo">
      <span class="title">后台管理系统</span>
    </div>
    <el-menu
      unique-opened
      :default-active="defaultValue"
      active-text-color="#409eff"
      background-color="#304156"
      text-color="#fff"
    >
      <template v-for="item in menuList" :key="item.id">
        <template v-if="item.type == 1">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item
                :index="subitem.id + ''"
                @click="handleMenuItemClick(subitem)"
              >
                <span>{{ subitem.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else-if="item.type == 2">
          <el-menu-item :index="item.id + ''">
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  
  </div>
</template>
<script setup>
import { ref } from '@vue/reactivity'
import { getCache } from '@/utils/cache.js'
import { useRoute } from 'vue-router'
import { pathMapMenu } from '../../utils/map-menu'
import router from '../../router'
import { computed } from '@vue/runtime-core'
import tagsStore from '../../store/modules/tags'

const store = tagsStore()
const menuList = ref([])
menuList.value = getCache('menuList')
const route = useRoute()

const currentPath = route.path
const currentMenuItem = store.currentMenu

const menu = pathMapMenu(menuList.value, currentPath)
const defaultValue = computed(() => {
  store.selectMenu(menu)
  return menu.id + ''
})
const handleMenuItemClick = (item) => {
  store.selectMenu(item)
  router.push({ path: item.url ?? '/not-found' })
}
</script>
<style lang="less" scoped>
.nav-menu {
  height: 100%;
  color: #bfcbd9;
}

.logo .title {
  display: block;
  padding: 10px;
}
</style>